package com.css.zfzx.fwsjcj.modules.analysis.service.impl;

import com.css.zfzx.fwsjcj.modules.analysis.repository.AnalysisMapNativeRepository;
import com.css.zfzx.fwsjcj.modules.analysis.service.AnalysisMapService;
import com.css.zfzx.fwsjcj.modules.analysis.vo.AnalysisMapVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: fwsjcj
 * @Author: lhl
 * @CreateDate: 2021/12/2 18:45
 */
@Service
public class AnalysisMapServiceImpl implements AnalysisMapService {
    @Autowired
    AnalysisMapNativeRepository nativeRepository;

    Map<String, Integer> pioneerMap=new HashMap<>();
    Map<String, AnalysisMapVO> voMap = new HashMap<>();
    @PostConstruct
    private void init(){
        initPioneerMap();
    }
    @Override
    public List<AnalysisMapVO> queryAnalysisMap() {
        List<Object[]> objectList = nativeRepository.queryAnalysisMap();
        getSupList(objectList);
        List<AnalysisMapVO> voList = new ArrayList<>();
        for (String key : voMap.keySet()){
            AnalysisMapVO vo = voMap.get(key);
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public List<AnalysisMapVO> queryPioneerMap() {
        List<Object[]> objectList = nativeRepository.queryAnalysisMap();
        getSupList(objectList);
        List<AnalysisMapVO> voList=new ArrayList<>();
        for (String key1 : pioneerMap.keySet()){
            BigDecimal targetArea= BigDecimal.valueOf(pioneerMap.get(key1)*10000);
            AnalysisMapVO vo=new AnalysisMapVO();
            String[] split = key1.split("-");
            vo.setProvince(split[0]);
            vo.setCity(split[1]);
            if (split.length==3){
                vo.setArea(split[2]);
            }
            vo.setSum(BigDecimal.ZERO);
            vo.setTargetArea(targetArea);
            vo.setPercent(BigDecimal.ZERO);
            boolean isNa=true;
            for (String key2 : voMap.keySet()){
                if (key2.contains(key1)){
                    isNa=false;
                    vo = voMap.get(key2);
                    vo.setTargetArea(targetArea);
                    if (targetArea.compareTo(vo.getSum()) > 0){
                        vo.setPercent(vo.getSum().divide(targetArea,8,BigDecimal.ROUND_HALF_UP));
                    }else {
                        vo.setPercent(BigDecimal.ONE);
                    }
                    voList.add(vo);
                    break;
                }
            }
            if (isNa) {
                voList.add(vo);
            }
        }
        return voList;
    }

    private Map<String, AnalysisMapVO> getSupList(List<Object[]> objectList) {
        voMap = new HashMap<>();
        for (Object[] objects : objectList) {
            String structureType = (String) objects[0];
            String province = (String) objects[1];
            String city = (String) objects[2];
            String area = (String) objects[3];
            objects[4] = objects[4] == null || "".equals(objects[4]) ? BigInteger.ZERO : objects[4];
            BigInteger count = (BigInteger) objects[4];
            objects[5] = objects[5] == null || "".equals(objects[5]) ? 0.0 : objects[5];
            Double num = (Double) objects[5];
            String key = province + "-" + city + "-" + area;
            AnalysisMapVO vo = null;
            if (voMap.containsKey(key)) {
                vo = voMap.get(key);
                vo.setCount(vo.getCount().add(count));
                vo.setSum(vo.getSum().add(BigDecimal.valueOf(num)));
            }else {
                vo = new AnalysisMapVO();
                vo.setProvince(province);
                vo.setCity(city);
                vo.setArea(area);
                vo.setCount(count);
                vo.setSum(BigDecimal.valueOf(num));
            }
            switch (structureType) {
                case "0": vo.setCount1(count);vo.setSum1(num);break;
                case "1": vo.setCount2(count);vo.setSum2(num);break;
                case "2": vo.setCount3(count);vo.setSum3(num);break;
                case "3": vo.setCount4(count);vo.setSum4(num);break;
                case "4": vo.setCount5(count);vo.setSum5(num);break;
                case "5": vo.setCount6(count);vo.setSum6(num);break;
                case "6": vo.setCount7(count);vo.setSum7(num);break;
                case "7": vo.setCount8(count);vo.setSum8(num);break;
                case "8": vo.setCount9(count);vo.setSum9(num);break;
            }
            voMap.put(key,vo);
        }
        return voMap;
    }

    private void initPioneerMap(){
        pioneerMap.put("河北省-邢台市", 220);
        pioneerMap.put("河北省-唐山市-滦州市", 23);
        pioneerMap.put("北京市-北京市-房山区", 75);
        pioneerMap.put("天津市-天津市-滨海新区", 110);
        pioneerMap.put("山西省-大同市", 150);
        pioneerMap.put("内蒙古自治区-赤峰市", 150);
        pioneerMap.put("辽宁省-锦州市", 150);
        pioneerMap.put("辽宁省-营口市", 150);
        pioneerMap.put("吉林省-吉林市", 220);
        pioneerMap.put("吉林省-白城市", 100);
        pioneerMap.put("黑龙江省-绥化市", 100);
        pioneerMap.put("江苏省-徐州市", 450);
        pioneerMap.put("江苏省-扬州市", 220);
        pioneerMap.put("浙江省-宁波市", 450);
        pioneerMap.put("安徽省-芜湖市", 220);
        pioneerMap.put("安徽省-六安市", 150);
        pioneerMap.put("福建省-厦门市", 450);
        pioneerMap.put("山东省-青岛市", 450);
        pioneerMap.put("山东省-菏泽市", 220);
        pioneerMap.put("山东省-日照市-岚山区", 30);
        pioneerMap.put("河南省-开封市", 150);
        pioneerMap.put("河南省-洛阳市", 220);
        pioneerMap.put("湖南省-岳阳市", 220);
        pioneerMap.put("广东省-珠海市", 220);
        pioneerMap.put("广东省-东莞市", 450);
        pioneerMap.put("广西壮族自治区-玉林市", 150);
        pioneerMap.put("海南省-儋州市", 70);
        pioneerMap.put("四川省-德阳市", 220);
        pioneerMap.put("四川省-乐山市", 150);
        pioneerMap.put("四川省-甘孜藏族自治州-康定市", 40);
        pioneerMap.put("贵州省-六盘水市", 70);
        pioneerMap.put("云南省-玉溪市", 150);
        pioneerMap.put("云南省-昭通市", 70);
        pioneerMap.put("西藏自治区-那曲市", 70);
        pioneerMap.put("陕西省-铜川市", 70);
        pioneerMap.put("陕西省-安康市", 70);
        pioneerMap.put("甘肃省-白银市", 70);
    }
}
